﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public partial class FrmLayerBrush : System.Windows.Forms.Form
	{
		public FrmLayerBrush()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
		}

		public FrmLayerBrush(Element elem, IList<Element> listElem, Document document)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.baseElem = elem;
			this.listElement = listElem;
			this.doc = document;
			this.InitialListString();
		}

		private void InitialListString()
		{
			this.listDiameter = new List<string>();
			this.listDiameter.Add("直径");
			this.listDiameter.Add("diameter");
			this.listWidth = new List<string>();
			this.listWidth.Add("宽度");
			this.listWidth.Add("width");
			this.listHeight = new List<string>();
			this.listHeight.Add("高度");
			this.listHeight.Add("height");
			this.listOffset = new List<string>();
			this.listOffset.Add("偏移量");
			this.listOffset.Add("offset");
			this.listType = new List<string>();
			this.listType.Add("管道类型");
			this.listLevel = new List<string>();
			this.listLevel.Add("参照标高");
			this.listLevel.Add("标高");
			this.listLevel.Add("level");
			this.listSysType = new List<string>();
			this.listSysType.Add("系统类型");
			this.listSysType.Add("systemtype");
		}

		private void FrmLayerBrush_Load(object sender, EventArgs e)
		{
			int num = this.InitialInterface();
			bool beHave = false;
			if (this.DrawDiameterText() | this.DrawPipingType() | this.DrawOffsetText() | this.DrawLevel() | this.DrawSyetemType() | this.DrawHeightText() | this.DrawWidthText())
			{
				beHave = true;
			}
			this.SettingInterface(num, beHave);
		}

		private int InitialInterface()
		{
			IEnumerable<string> parametersName = new LayerBrushSetting().GetParametersName(this.baseElem, this.listElement);
			int num = 20;
			foreach (string text in parametersName)
			{
				CheckBox checkBox = new CheckBox();
                System.Drawing.Point location = new System.Drawing.Point(10, num);
				checkBox.Text = text;
				checkBox.Location = location;
				checkBox.Checked = false;
				checkBox.Width = 100;
				this.groupBox1.Controls.Add(checkBox);
				checkBox.CheckedChanged += this.ckBox_Changed;
				num += 22;
			}
			return num;
		}

		private int BtnSureEnabled()
		{
			int num = 0;
			foreach (object obj in this.groupBox1.Controls)
			{
				CheckBox checkBox = obj as CheckBox;
				if (checkBox != null && checkBox.Checked)
				{
					num++;
				}
			}
			return num;
		}

		private void SettingInterface(int num, bool beHave)
		{
			int num2;
			int width;
			int num3;
			if (beHave)
			{
				num2 = 250;
				width = 220;
				num3 = 170;
			}
			else
			{
				num2 = 150;
				width = 120;
				num3 = 75;
			}
			if (RevitVersion.Version == "2014")
			{
				base.Width = num2;
				base.Height = num + 90;
			}
			else
			{
				base.Width = num2 + 10;
				base.Height = num + 100;
			}
			this.groupBox1.Width = width;
			this.groupBox1.Height = num + 10;
			Button button = new Button();
            System.Drawing.Point location = new System.Drawing.Point(num3 - 70, num + 30);
			button.Text = "确定";
			button.Width = 65;
			button.Height = 25;
			button.Name = "btnFix";
			button.Enabled = false;
			button.Location = location;
			base.Controls.Add(button);
			button.Click += this.btnSure_Click;
			Button button2 = new Button();
            System.Drawing.Point location2 = new System.Drawing.Point(num3, num + 30);
			button2.Text = "取消";
			button2.Width = 65;
			button2.Height = 25;
			button2.Location = location2;
			base.Controls.Add(button2);
			button2.Click += this.btnCancel_Click;
			base.CancelButton = button2;
			base.AcceptButton = button;
		}

		private bool DrawDiameterText()
		{
			return this.DrawText(this.listDiameter, "txtDiameter");
		}

		private bool DrawWidthText()
		{
			return this.DrawText(this.listWidth, "txtWidth");
		}

		private bool DrawHeightText()
		{
			return this.DrawText(this.listHeight, "txtHeight");
		}

		private bool DrawOffsetText()
		{
			return this.DrawText(this.listOffset, "txtOffset");
		}

		private bool DrawText(IList<string> listTxtName, string txtName)
		{
			bool result = false;
			foreach (object obj in this.groupBox1.Controls)
			{
				CheckBox checkBox = obj as CheckBox;
				if (checkBox != null && this.BeContainsText(listTxtName, checkBox.Text))
				{
					TextBox textBox = new TextBox();
                    System.Drawing.Point location = checkBox.Location;
                    System.Drawing.Point location2 = new System.Drawing.Point(location.X + 100, location.Y);
                    string str = Convert.ToString(UnitConvert.CovertFromAPI((DisplayUnitType)2, this.baseElem.GetParameter(checkBox.Text).AsDouble()));
					textBox.Text = StringTools.CutOffEfficacious(str, 1) + "mm";
					textBox.Name = txtName;
					textBox.Width = 100;
					textBox.Height = 20;
					textBox.Location = location2;
					textBox.Tag = checkBox.Text;
					textBox.Enabled = false;
					this.groupBox1.Controls.Add(textBox);
					textBox.Leave += this.txt_Leave;
					result = true;
				}
			}
			return result;
		}

		private bool BeContainsText(IList<string> listStr, string strText)
		{
			return listStr.Contains(strText) || listStr.Contains(strText.ToLower());
		}

		private bool DrawPipingType()
		{
			bool result = false;
			if (this.baseElem is Pipe)
			{
				result = this.DrawCombox(this.listType, "cmbPipingType", YJKElementFilter.GetElementByClass(typeof(PipeType), this.doc), (this.baseElem as Pipe).PipeType.Name);
			}
			if (this.baseElem is Duct)
			{
				result = this.DrawCombox(this.listType, "cmbPipingType", YJKElementFilter.GetElementByClass(typeof(DuctType), this.doc), (this.baseElem as Duct).DuctType.Name);
			}
			return result;
		}

		private bool DrawLevel()
		{
			bool result = false;
			Parameter parameter = this.baseElem.GetParameter(BuiltInParameter.RBS_START_LEVEL_PARAM);
			if (parameter == null)
			{
				parameter = this.baseElem.GetParameter(BuiltInParameter.FAMILY_LEVEL_PARAM);
			}
			if (parameter != null && parameter.HasValue)
			{
				string name = parameter.Definition.Name;
				result = this.DrawCombox(this.listLevel, "cmbLevel", YJKElementFilter.GetElementByClass(typeof(Level), this.doc), parameter.AsValueString());
			}
			return result;
		}

		private bool DrawSyetemType()
		{
			bool result = false;
			if (this.baseElem is Pipe)
			{
				Parameter parameter = (this.baseElem as Pipe).GetParameter(BuiltInParameter.RBS_PIPING_SYSTEM_TYPE_PARAM);
				result = this.DrawCombox(this.listSysType, "cmbsysType", YJKElementFilter.GetElementByClass(typeof(PipingSystemType), this.doc), parameter.AsValueString());
			}
			if (this.baseElem is Duct)
			{
				Parameter parameter = (this.baseElem as Duct).GetParameter(BuiltInParameter.RBS_DUCT_SYSTEM_TYPE_PARAM);
				result = this.DrawCombox(this.listSysType, "cmbsysType", YJKElementFilter.GetElementByClass(typeof(MechanicalSystemType), this.doc), parameter.AsValueString());
			}
			return result;
		}

		private bool DrawCombox(IList<string> listName, string cmbName, List<Element> listElem, string selectedText)
		{
			bool result = false;
			foreach (object obj in this.groupBox1.Controls)
			{
				CheckBox checkBox = obj as CheckBox;
				if (checkBox != null && this.BeContainsText(listName, checkBox.Text))
				{
					ComboBox comboBox = new ComboBox();
                    System.Drawing.Point location = checkBox.Location;
                    System.Drawing.Point location2 = new System.Drawing.Point(location.X + 100, location.Y);
					comboBox.Width = 100;
					comboBox.Height = 20;
					comboBox.Location = location2;
					comboBox.Name = cmbName;
					comboBox.Tag = checkBox.Text;
					comboBox.Enabled = false;
					this.groupBox1.Controls.Add(comboBox);
					comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
					this.BandCombox(listElem, comboBox, selectedText);
					result = true;
				}
			}
			return result;
		}

		private void BandCombox(List<Element> listElem, ComboBox comBox, string cbName)
		{
			List<BindType> list = new List<BindType>();
			int selectedIndex = 0;
			int num = 0;
			foreach (Element element in listElem)
			{
				list.Add(new BindType
				{
					BindName = element.Name,
					BindId = element.Id
				});
				if (!string.IsNullOrEmpty(cbName) && cbName == element.Name)
				{
					selectedIndex = num;
				}
				num++;
			}
			comBox.DataSource = list;
			comBox.ValueMember = "BindId";
			comBox.DisplayMember = "BindName";
			comBox.SelectedIndex = selectedIndex;
		}

		private void txt_Leave(object sender, EventArgs e)
		{
			TextBox textBox = sender as TextBox;
			bool flag = false;
			try
			{
				string text = textBox.Text;
				if (text.Contains("m"))
				{
					text = text.Substring(0, text.IndexOf("m"));
				}
				if (textBox.Name == "txtDiameter" || textBox.Name == "txtWidth" || textBox.Name == "txtHeight")
				{
					int num = Convert.ToInt32(text);
					if (num > 0 && num < 10000)
					{
						flag = true;
						textBox.Text = num + "mm";
					}
				}
				if (textBox.Name == "txtOffset")
				{
					double num2 = Convert.ToDouble(text);
					if (num2 > 0.0)
					{
						flag = true;
						textBox.Text = num2 + "mm";
					}
				}
			}
			catch (Exception)
			{
				flag = false;
			}
			if (!flag)
			{
				if (textBox.Name == "txtDiameter" || textBox.Name == "txtWidth" || textBox.Name == "txtHeight")
				{
					YJKMessageBox.Show("管道尺寸必须为正整数，\n且不能大于10000");
				}
				if (textBox.Name == "txtOffset")
				{
					YJKMessageBox.Show("偏移量必须为数字！");
				}
				textBox.Focus();
				textBox.SelectAll();
				return;
			}
		}

		private void ckBox_Changed(object sender, EventArgs e)
		{
			CheckBox checkBox = sender as CheckBox;
			foreach (object obj in this.groupBox1.Controls)
			{
                System.Windows.Forms.Control control = (System.Windows.Forms.Control)obj;
				if (control != null && control.Tag != null && !(checkBox.Text != control.Tag.ToString()))
				{
					if (checkBox.Checked)
					{
						control.Enabled = true;
					}
					else
					{
						control.Enabled = false;
					}
				}
			}
			if (this.BtnSureEnabled() < 1)
			{
				IEnumerator enumerator = base.Controls.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						object obj2 = enumerator.Current;
						Button button = ((System.Windows.Forms.Control)obj2) as Button;
						if (button != null && !string.IsNullOrEmpty(button.Name) && !(button.Name != "btnFix"))
						{
							button.Enabled = false;
						}
					}
					return;
				}
			}
			foreach (object obj3 in base.Controls)
			{
				Button button2 = ((System.Windows.Forms.Control)obj3) as Button;
				if (button2 != null && !string.IsNullOrEmpty(button2.Name) && !(button2.Name != "btnFix"))
				{
					button2.Enabled = true;
				}
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnSure_Click(object sender, EventArgs e)
		{
			Transaction transaction = new Transaction(this.doc, "属性刷设置");
			transaction.Start();
			this.SetLevelValue();
			foreach (object obj in this.groupBox1.Controls)
			{
				CheckBox checkBox = obj as CheckBox;
				if (checkBox != null && checkBox.Checked)
				{
					foreach (Element otherElem in this.listElement)
					{
						LayerBrushSetting layerBrushSetting = new LayerBrushSetting(this.baseElem, otherElem);
						if (this.BeContainsText(this.listDiameter, checkBox.Text))
						{
							layerBrushSetting.SetParameter(checkBox.Text, this.GetTextValue("txtDiameter"));
						}
						else if (this.BeContainsText(this.listHeight, checkBox.Text))
						{
							layerBrushSetting.SetParameter(checkBox.Text, this.GetTextValue("txtHeight"));
						}
						else if (this.BeContainsText(this.listWidth, checkBox.Text))
						{
							layerBrushSetting.SetParameter(checkBox.Text, this.GetTextValue("txtWidth"));
						}
						else if (!this.BeContainsText(this.listLevel, checkBox.Text) && !this.BeContainsText(this.listOffset, checkBox.Text))
						{
							if (this.BeContainsText(this.listSysType, checkBox.Text))
							{
								layerBrushSetting.SetParameter(checkBox.Text, this.GetComboxValue("cmbsysType"));
							}
							else if (checkBox.Text == "管道类型" && checkBox.Checked)
							{
								layerBrushSetting.SpecialProperty(this.GetComboxValue("cmbPipingType"));
							}
							else
							{
								layerBrushSetting.SetParameter(checkBox.Text);
							}
						}
					}
				}
			}
			this.doc.Regenerate();
			transaction.Commit();
			base.Close();
		}

		private void SetLevelValue()
		{
			int num = 0;
			foreach (object obj in this.groupBox1.Controls)
			{
				CheckBox checkBox = obj as CheckBox;
				if (checkBox != null && checkBox.Checked)
				{
					if (this.BeContainsText(this.listOffset, checkBox.Text))
					{
						num++;
					}
					if (!this.BeContainsText(this.listLevel, checkBox.Text))
					{
						num++;
					}
				}
			}
			if (num == 2)
			{
				foreach (object obj2 in this.groupBox1.Controls)
				{
					CheckBox checkBox2 = obj2 as CheckBox;
					if (checkBox2 != null && checkBox2.Checked)
					{
						foreach (Element otherElem in this.listElement)
						{
							LayerBrushSetting layerBrushSetting = new LayerBrushSetting(this.baseElem, otherElem);
							if (this.BeContainsText(this.listLevel, checkBox2.Text))
							{
								layerBrushSetting.SetParameter(checkBox2.Text, this.GetComboxValue("cmbLevel"));
							}
						}
					}
				}
				IEnumerator enumerator = this.groupBox1.Controls.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						object obj3 = enumerator.Current;
						CheckBox checkBox3 = obj3 as CheckBox;
						if (checkBox3 != null && checkBox3.Checked)
						{
							foreach (Element otherElem2 in this.listElement)
							{
								LayerBrushSetting layerBrushSetting2 = new LayerBrushSetting(this.baseElem, otherElem2);
								if (this.BeContainsText(this.listOffset, checkBox3.Text))
								{
									layerBrushSetting2.SetParameter(checkBox3.Text, this.GetTextValue("txtOffset"));
								}
							}
						}
					}
					return;
				}
			}
			foreach (object obj4 in this.groupBox1.Controls)
			{
				CheckBox checkBox4 = obj4 as CheckBox;
				if (checkBox4 != null && checkBox4.Checked)
				{
					foreach (Element otherElem3 in this.listElement)
					{
						LayerBrushSetting layerBrushSetting3 = new LayerBrushSetting(this.baseElem, otherElem3);
						if (this.BeContainsText(this.listLevel, checkBox4.Text))
						{
							layerBrushSetting3.SetParameter(checkBox4.Text, this.GetComboxValue("cmbLevel"));
						}
						else if (this.BeContainsText(this.listOffset, checkBox4.Text))
						{
							layerBrushSetting3.SetParameter(checkBox4.Text, this.GetTextValue("txtOffset"));
						}
					}
				}
			}
		}

		private string GetTextValue(string txtName)
		{
			string text = "";
			foreach (object obj in this.groupBox1.Controls)
			{
                System.Windows.Forms.Control control = (System.Windows.Forms.Control)obj;
				if (control != null && !(control.Name != txtName))
				{
					TextBox textBox = control as TextBox;
					if (textBox != null)
					{
						text = textBox.Text;
						if (text.Contains("m"))
						{
							text = text.Substring(0, text.IndexOf("m"));
						}
					}
				}
			}
			return text;
		}

		private object GetComboxValue(string txtName)
		{
			object result = "";
			foreach (object obj in this.groupBox1.Controls)
			{
                System.Windows.Forms.Control control = (System.Windows.Forms.Control)obj;
				if (control != null && !(control.Name != txtName))
				{
					ComboBox comboBox = control as ComboBox;
					if (comboBox != null)
					{
						result = comboBox.SelectedValue;
					}
				}
			}
			return result;
		}

		private Document doc;

		private Element baseElem;

		private IList<Element> listElement;

		private IList<string> listDiameter;

		private IList<string> listWidth;

		private IList<string> listHeight;

		private IList<string> listOffset;

		private IList<string> listType;

		private IList<string> listLevel;

		private IList<string> listSysType;
	}
}
